package com.example.sixthweek;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapTask implements Runnable{
    private static final String TAG = "myclick";
    private Handler handler;
    public void setHandler(Handler handler){
        this.handler = handler;
    }
    @Override
    public void run() {
        //获取网络数据并返回

            Log.i(TAG, "run: Running....");
            //do...
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ArrayList<HashMap<String ,String>> rateList = new ArrayList<HashMap<String,String>>();
            try {
                Document doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
//                String title = doc.title();
//                Log.i(TAG, "run: title=" + title);
//                Elements trs = doc.getElementsByTag("tr");
//                for (Element tr : trs) {
//                    Elements tds = tr.getElementsByTag("td");
//                    Log.i(TAG, "run: td.size=" + tds.size());
//                    if (tds.size() > 0) {
//                        String td1 = tds.get(0).text();
//                        String td2 = tds.get(5).text();
//                        rateList.add(td1+"==>"+td2);
//                    }
//                }
//                Elements tds = doc.getElementsByTag("td");
//                for(int i=8;i < tds.size();i+=8){
//                      String td1 = tds.get(i).text();
//                      String td2 = tds.get(i+5).text();
//                      Log.i(TAG, "run: "+ td1+"==>"+td2);
//                      rateList.add(td1 + "==>" + td2);
//                   }
//                Log.i(TAG, "run: "+rateList);
                Elements tables = doc.getElementsByTag("table");
                Element table2 = tables.get(1);
                Elements trs = table2.getElementsByTag("tr");
                for (Element tr : trs){
                    Elements tds = tr.getElementsByTag("td");
                    if(tds.size()>0){
                        String td1 = tds.get(0).text();
                        String td2 = tds.get(5).text();
                        Log.i(TAG, "run: "+ td1 + "==>" + td2);
//                        rateList.add(td1 + "==>" + td2);
                        HashMap<String,String> map = new HashMap<String,String>();
                        map.put("cname",td1);
                        map.put("cval",td2);
                        rateList.add(map);
                    }
                }

                Message msg = handler.obtainMessage(5);
                msg.obj = rateList;
                handler.sendMessage(msg);


            } catch (IOException e) {
                e.printStackTrace();
            }




        }
    }

